package com.oamicnet.controller;

import com.oamicnet.entity.Student;
import com.oamicnet.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping("/save")
    public String saveStudent(Student student) {
        studentService.save(student);
        return "redirect:/student/list";
    }

    @PostMapping("/update")
    public String updateStudent(Student student) {
        studentService.update(student);
        return "redirect:/student/list";
    }

    @GetMapping("/delete")
    public String deleteStudentById(@RequestParam("id") Integer id) {
        studentService.delete(id);
        return "redirect:/student/list";
    }

    @GetMapping("/list")
    public String getAllStudents(Model model) {
        List<Student> studentList = studentService.findAll();
        model.addAttribute("studentList", studentList);
        return "student/list-students";
    }

    //跳转
    @GetMapping("/add")
    public String add() {
        return "student/add-student";
    }

    //跳转
    @GetMapping("/edit")
    public String edit(@RequestParam("id") Integer id, Model model) {
        Student student = studentService.findById(id);
        model.addAttribute("student", student);
        return "student/edit-student";
    }
}
